package com.hsyt.pms.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pfc.support.util.EnumToDataUtil;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.PltAuth;
import com.hsyt.pms.api.common.PltLog;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.system.SysParamDto;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.enums.DataType;
import com.hsyt.pms.api.enums.SysType;
import com.hsyt.pms.api.vo.system.SysParamVo;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.sys.dubbo.SysDictionaryService;
import com.hsyt.pms.sys.dubbo.SysParamService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.Date;

/**
 * @ClassName: SysParamController
 * @Description: 系统参数服务控制层
 * @author: wangze@hsyuntai.com
 * @date: 2019/1/29 18:19
 */
@RestController
@RequestMapping("/pms/system/param")
public class SysParamController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysParamController.class);

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysParamService sysParamService;

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysDictionaryService sysDictionaryService;

    @RequestMapping(value = "/page")
    @PltAuth(auth ="ROLE_PARAM_MG")
    public ModelAndView page(ModelMap model) {
        String sysTypeComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(SysType.class);
        model.put("sysTypeComboDatas", sysTypeComboData);
        String dataTypeComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(DataType.class);
        model.put("dataTypeComboData", dataTypeComboData);
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("system/pageParam", model);
    }

    @PltLog(log = "查询参数管理列表信息")
    @PltAuth(auth ="ROLE_PARAM_MG")
    @RequestMapping(value = "/findPageData")
    public @ResponseBody
    PageResult findPageData(@RequestBody SysParamDto dto) {
        PageResult<SysParamVo> pageResult = new PageResult<SysParamVo>();
        try {
            pageResult = sysParamService.searchPageDataByDto(dto);
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("SysParamController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(LOGGER, message);
        }
        return pageResult;
    }

    @PltAuth(auth ="ROLE_PARAM_MG_MODIFY")
    @RequestMapping(value = "/toEditForm")
    public ModelAndView toEditForm(ModelMap model, @RequestParam("formType") String formType) {
        model.put("formType", formType);
        return new ModelAndView("system/formParam");
    }

    @PltLog(log = "启用/停用参数")
    @PltAuth(auth ="ROLE_PARAM_MG_OPT")
    @RequestMapping(value = "/doUndo")
    public @ResponseBody
    ResultMessage doUndo(@RequestBody SysParamDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "操作成功");
        try {
            dto.setGmtModify(new Date());
            this.sysParamService.updateByPrimaryKey(dto);
        } catch (Exception e) {
            result.change(false, "操作失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysParam-doUndo error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

    @PltLog(log = "编辑参数")
    @PltAuth(auth ="ROLE_PARAM_MG_MODIFY")
    @RequestMapping(value = "/editSysParam")
    public @ResponseBody
    ResultMessage editSysParam(@RequestBody SysParamDto sysParamDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "更新成功");
        try {
            sysParamDto.setGmtModify(new Date());
            this.sysParamService.updateByPrimaryKey(sysParamDto);
        } catch (Exception e) {
            result.change(false, "更新失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-editSysDict error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "保存参数")
    @PltAuth(auth ="ROLE_PARAM_MG_MODIFY")
    @RequestMapping(value = "/save")
    public @ResponseBody
    ResultMessage save(@RequestBody SysParamDto dto, String formType, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            if (dto.getId() == null) {

                if (StringUtils.isBlank(dto.getStatus())) {
                    dto.setStatus(AbleStatus.ENABLED.getCode());
                }
                sysParamService.addParam(dto);
            } else {
                sysParamService.updateByPrimaryKey(dto);
            }
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysParamController-save error: {0}", e.getMessage());
        }
        return result;
    }

}
